using System;
using System.Collections.Generic;
using System.Text;

namespace CasinoServer.BusinessEntities
{
    public class tmpPokerPlayer
    {
        #region members
        private bool? ReadyToPlay;
        private int iDField;
        private tmpCard card1Field;
        private tmpCard card2Field;
        private int actualBetField;
        private tmpAction currentPlayerActionField;
        private int moneyField;
        private int ChairIdField;
        #endregion

        public tmpPokerPlayer(int idPlayer, int PlayerMoney, int chairId)
        {
            this.iDField = idPlayer;
            this.moneyField = PlayerMoney;
            this.ReadyToPlay = false;
            this.ChairIdField = chairId;
            this.card1Field = null;
            this.card2Field = null;
        }

        #region properties

        public int _PlayerId
        {
            get { return iDField; }
        }

        public tmpAction _CurrentAction
        {
            get { return currentPlayerActionField; }
        }

        public int _PlayerMoney
        {
            get { return moneyField; }
        }

        public int _ChairId
        {
            get { return ChairIdField; }
        }

        public bool isReady
        {
            get
            {
                if (ReadyToPlay.Value)
                    return ReadyToPlay.Value;
                else
                    throw new ArgumentException("Ready field was null");
            }
        }

        #endregion

        #region methods

        public int Bet(int betCash)
        {
            if (moneyField < betCash)
                throw new tmpPokerException(tmpPokerException.PokerExceptionType.NotEnaughCashToBet_HasToGoAllIn);
            actualBetField += betCash;
            moneyField -= betCash;
            this.currentPlayerActionField = tmpAction.Bet;
            return betCash;
        }

        public void GiveCard(tmpCard card)
        {
            if(card1Field==null)
                this.card1Field = card;
            else
            this.card2Field = card;

            this.currentPlayerActionField = tmpAction.Card;
        }

        public tmpCard ShowCardOne
        {
            get { return card1Field; }
        }

        public tmpCard ShowCardTwo
        {
            get { return card2Field; }
        }

        public void Pass()
        {
            this.currentPlayerActionField = tmpAction.Pass;
        }

        public void Win(int moneyWin)
        {
            if (actualBetField != 0)
                throw new tmpPokerException(tmpPokerException.PokerExceptionType.PlayerWonButActualBetWasNotZero);
            moneyField += moneyWin;
            this.currentPlayerActionField = tmpAction.Win;
        }

        public void Loose()
        {
            if (actualBetField != 0)
                throw new tmpPokerException(tmpPokerException.PokerExceptionType.PlayerLostButActualBetWasNotZero);
            this.currentPlayerActionField = tmpAction.Lost;
        }

        public void Wait()
        {
            this.currentPlayerActionField = tmpAction.Wait;
        }

        public int Check(int currentStake)
        {
            if (moneyField < (currentStake - actualBetField))
                throw new tmpPokerException(tmpPokerException.PokerExceptionType.NotEnaughCashToBet_HasToGoAllIn);
            actualBetField += (currentStake - actualBetField);
            moneyField -= (currentStake - actualBetField);
            this.currentPlayerActionField = tmpAction.Check;
            return actualBetField;
        }

        public void GetReadyToPlay()
        {
            ReadyToPlay = true;
        }

        public void PrepareForNewRound()
        {
            if (actualBetField != 0)
                throw new tmpPokerException(tmpPokerException.PokerExceptionType.NewRoundButPlayerStillHasActualBet);
            
            this.card1Field = null;
            this.card2Field = null;
            this.currentPlayerActionField = tmpAction.NewRound;
        }

        #endregion
    }
}
